package com.turboconnard.display;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.ladydinde.R;
import com.turboconnard.geom.Point3D;
import com.turboconnard.tools.Textures;

public class Ribbon extends Stroke3D {
	private float[] textures;

	/** The initial indices definition */
	private byte indices[];// = {0,2,1,3,1,2};
	private float _height;
	private float STROKE_MAX = 40;
	private float STROKE_MIN = 5;

	private float DIST_MAX = 120;
	private float DIST_MIN = 10;

	private FloatBuffer textureBuffer;
	private ByteBuffer textByteBuf;
	private ByteBuffer indexBuffer;
	private int texture;

	private float stroke;
	// private byte[] indices;

	public float scale;

	public Ribbon(float pHeight) {
		super();
		mode = /* GL10.GL_LINE_STRIP;// */GL10.GL_TRIANGLE_STRIP;
		// GL10.GL_MAX_ELEMENTS_VERTICES = 10;
		texture = R.drawable.caisse1;
		//
		stroke = STROKE_MIN + (STROKE_MAX - STROKE_MIN) / 2;
	}

	public void addPoints(float[][] pPoints) {
		points = new float[pPoints.length * 2 * 3];
		int c = 0;
		Point3D oldPoint = null;
		for (int k = 0; k < pPoints.length; k++) {
			Point3D newPoint = new Point3D(pPoints[k][0], pPoints[k][1],pPoints[k][2]);
			if (k == 0) {
				oldPoint = new Point3D(0, 0, 0);
			}
			double a;
			float dist;
			a =  Math.atan2(newPoint.y - oldPoint.y, newPoint.x - oldPoint.x);
			dist = (float) Math.sqrt(Math.pow(newPoint.y - oldPoint.y, 2)
					+ Math.pow(newPoint.x - oldPoint.x, 2));
			stroke = 35;// (dist - stroke)/2;

			points[c] = (newPoint.x  - ( (float) (Math.cos(a + Math.PI / 2)*4*stage.correction )))*2;
			points[c + 1] = -(newPoint.y + ( (float) (Math.sin(a - Math.PI / 2)*4*stage.correction )))*2;
			points[c + 2] = 0;// newPoint.z/10;
			c += 3;

			points[c] = (newPoint.x -((float) (Math.cos(a - Math.PI / 2) *4*stage.correction )))*2;
			points[c + 1] = -(newPoint.y +((float) (Math.sin(a + Math.PI / 2)*4*stage.correction )))*2;
			points[c + 2] = 0;// newPoint.z/10 ;
			c += 3;
			oldPoint = newPoint;
		}

		byteBuf = ByteBuffer.allocateDirect(points.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());

		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.clear();
		vertexBuffer.put(points);
		vertexBuffer.position(0);
	}

	@Override
	public void addPoint(float pX, float pY, float pZ) {
		vPoints.add(new Point3D(pX, pY, pZ));
		// float[] vert = new float[vPoints.size()*3];
		count++;
		// if(count<=1) return;
		points = new float[count * 6];
		// indices = new byte[count*6];
		// textures = new float[count*12];
		// private byte indices[] = {0,2,1, 3,1,2};

		int c = 0;
		int t = 0;
		//	
		/*
		 * textures[0] = 0.0f; textures[1] = 0.0f; textures[2] = 0.0f;
		 * textures[3] = 1.0f;
		 */
		t = 4;
		Point3D oldPoint = null;
		for (int k = 0; k < vPoints.size(); k++) {
			Point3D newPoint = vPoints.get(k);
			if (k == 0) {
				oldPoint = new Point3D(0, 0, 0);
			}
			double a;
			float dist;
			a = Math.atan2(newPoint.y - oldPoint.y, newPoint.x - oldPoint.x);
			dist = (float) Math.sqrt(Math.pow(newPoint.y - oldPoint.y, 2)
					+ Math.pow(newPoint.x - oldPoint.x, 2));

			// if (dist > DIST_MAX) dist = DIST_MAX;
			// if (dist < DIST_MIN) dist = DIST_MIN;

			stroke = 35;// (dist - stroke)/2;

			// float ph = ( dist * (STROKE_MAX - STROKE_MIN)) /(DIST_MAX -
			// DIST_MIN);// ;
			/*
			 * 
			 * if(k==0){ //old point points[c] = (oldPoint.x - (float)
			 * Math.cos(a + Math.PI / 2) * (ph / 2))*stage.correction; points[c
			 * + 1] = (oldPoint.y + (float) Math.sin(a - Math.PI / 2)* (ph /
			 * 2))* stage.correction; points[c + 2] = oldPoint.z *
			 * stage.correction; points[c + 3] = (oldPoint.x - (float)
			 * Math.cos(a - Math.PI / 2) * (ph / 2))* stage.correction; points[c
			 * + 4] = (oldPoint.y + (float) Math.sin(a + Math.PI / 2)* (ph /
			 * 2))* stage.correction; points[c + 5] = oldPoint.z *
			 * stage.correction; c += 6; }else{ points[c] = points[c-6];
			 * points[c + 1] = points[c-5]; points[c + 2] = points[c-4];
			 * points[c + 3] = points[c-3]; points[c + 4] = points[c-2];
			 * points[c + 5] = points[c-1]; c += 6; }
			 */
			// new point

			points[c] = (newPoint.x - (float) Math.cos(a + Math.PI / 2)	* ((STROKE_MAX - stroke) / 2))					* stage.correction;
			points[c + 1] = -(newPoint.y + (float) Math.sin(a - Math.PI / 2)	* ((STROKE_MAX - stroke) / 2))	* stage.correction;
			points[c + 2] = newPoint.z * stage.correction;
			c += 3;

			points[c] = (newPoint.x - (float) Math.cos(a - Math.PI / 2)	* ((STROKE_MAX - stroke) / 2))	* stage.correction;
			points[c + 1] = -(newPoint.y + (float) Math.sin(a + Math.PI / 2)* (stroke / 2))
					* stage.correction;
			points[c + 2] = newPoint.z * stage.correction;
			c += 3;

			/*
			 * if(k%2 == 0){ textures[t] = 1.0f; textures[t+1] = 0.0f;
			 * textures[t+2] = 1.0f; textures[t+3] = 1.0f; }else{ textures[t] =
			 * 0.0f; textures[t+1] = 0.0f; textures[t+2] = 0.0f; textures[t+3] =
			 * 1.0f;
			 * 
			 * }
			 */
			t += 4;
			/*
			 * indices[k*6] = (byte)((k*4)+0); indices[k*6+1] = (byte)((k*4)+2);
			 * indices[k*6+2] = (byte)((k*4)+1);
			 * 
			 * indices[k*6+3] = (byte)((k*4)+3); indices[k*6+4] =
			 * (byte)((k*4)+1); indices[k*6+5] = (byte)((k*4)+2);
			 */
			oldPoint = newPoint;
		}

		byteBuf = ByteBuffer.allocateDirect(points.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());

		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.clear();
		vertexBuffer.put(points);
		vertexBuffer.position(0);
		/*
		 * textByteBuf = ByteBuffer.allocateDirect(textures.length * 4);
		 * textByteBuf.order(ByteOrder.nativeOrder());
		 * 
		 * textureBuffer = textByteBuf.asFloatBuffer();
		 * textureBuffer.put(textures); textureBuffer.position(0);
		 */
		/*
		 * indexBuffer = ByteBuffer.allocateDirect(indices.length);
		 * indexBuffer.put(indices); indexBuffer.position(0);
		 */
	}

	@Override
	protected void _draw(GL10 gl) {
		if (points == null)
			return;
		if (points.length <= 1)
			return;
		//gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
		gl.glEnable(GL10.GL_BLEND);
		//gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		/*
		 * gl.glEnable(GL10.GL_TEXTURE_2D); gl.glBindTexture(GL10.GL_TEXTURE_2D,
		 * Textures.getInstance().getTexture(texture));
		 */
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glFrontFace(GL10.GL_CCW);
		/*
		 * gl.glPushMatrix(); gl.glTranslatef(0.5f, points[1], points[2]);
		 */
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		// gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
		gl.glDrawArrays(mode, 0, points.length / 3);
		// gl.glPopMatrix();
		// gl.glDisable(GL10.GL_TEXTURE_2D);
		// gl.glDisable(GL10.GL_BLEND);
		// gl.glDisable(GL10.GL_DEPTH_TEST);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
	}
}
